/**
 * MySwing: Advanced Swing Utilites
 * Copyright (C) 2005  Santhosh Kumar T
 * <p/>
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * <p/>
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 */

package com.viweb.storemanager.core;

import java.awt.Dialog;
import java.awt.Frame;
import java.awt.HeadlessException;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import javax.swing.BorderFactory;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.SwingUtilities;
import javax.swing.border.BevelBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class ProgressDialog extends JDialog implements ChangeListener {
    JLabel statusLabel = new JLabel();
    JProgressBar progressBar;
    ProgressMonitor monitor;
    int progressStyle = 0;
    // Các biến lưu giữ toạ độ
    // hiện thời của con trỏ chuột
    int x=-1;
    int y=-1;
    
    public ProgressDialog(Frame owner, ProgressMonitor monitor) throws HeadlessException{
        super(owner, "Trạng thái", true);
        init(monitor);
    }
    
    public ProgressDialog(Dialog owner, ProgressMonitor monitor) throws HeadlessException{
        super(owner);
        init(monitor);
    }
    
    private void init(ProgressMonitor monitor){
        this.monitor = monitor;
        
        progressBar = new JProgressBar(0, monitor.getTotal());
        progressBar.setUI(new KumarProgressBarUI());
        
        if (monitor.isIndeterminate()) {
            progressBar.setIndeterminate(true);
            
        } else {
            progressBar.setValue(monitor.getCurrent());
        }
        
        statusLabel.setText(monitor.getStatus());
        
        JPanel contents = (JPanel) getContentPane();
        // contents.setBorder(BorderFactory.createEtchedBorder());
        contents.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
        
        // Xử lý di chuyển dialog
        contents.addMouseListener(new MouseAdapter(){
            public void mouseReleased(MouseEvent e){
                x = e.getX();
                y = e.getY();
            }
            public void mousePressed(MouseEvent e){
                x = e.getX();
                y = e.getY();
            }
        });
        contents.addMouseMotionListener(new MouseMotionAdapter(){
            public void mouseDragged(MouseEvent e){
                int xx = getLocation().x;
                int yy = getLocation().y;
                setLocation(xx + (e.getX() - x), yy + (e.getY() - y));
            }
        });
        
        java.awt.GridBagConstraints gridBagConstraints;
        contents.setLayout(new java.awt.GridBagLayout());
        
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(4, 5, 0, 5);
        add(statusLabel, gridBagConstraints);
        
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(4, 5, 5, 5);
        add(progressBar, gridBagConstraints);
        
        setUndecorated(true);
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        setResizable(false);
        monitor.addChangeListener(this);
    }
    
    public void stateChanged(final ChangeEvent ce){
        // to ensure EDT thread
        if (!SwingUtilities.isEventDispatchThread()){
            
            SwingUtilities.invokeLater(new Runnable(){
                public void run(){
                    stateChanged(ce);
                }
            });
            return;
        }
        
        if (monitor.getCurrent() != monitor.getTotal()){
            
            statusLabel.setText(monitor.getStatus());
            if (!monitor.isIndeterminate()) {
                progressBar.setValue(monitor.getCurrent());
            }
            
        } else {
            dispose();
        }
    }
}